package com.zh.business.base.entity;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

/**
 * 实体类基类
 *
 * @author walle
 */
@Data
public class BasicEntity implements Serializable {

    private static final long serialVersionUID = -7682878941982463356L;

    /**
     * 主键
     */
    @Id
    @Column(name = "id")
    @ApiModelProperty(value = "主键,前台不传")
    private String id;

    /**
     * 创建人
     */
    @Column(name = "create_by")
    @JsonIgnore
    @ApiModelProperty(value = "创建人,前台不传")
    private Integer createBy;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间,格式：yyyy-MM-dd HH:mm:ss。前台不传")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 更新人
     */
    @Column(name = "update_by")
    @JsonIgnore
    @ApiModelProperty(value = "更新人,前台不传")
    private Integer updateBy;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "更新时间,格式：yyyy-MM-dd HH:mm:ss。前台不传")
    private Date updateTime;

    /**
     * 逻辑删除标记
     */
    @Column(name = "delete_flag")
    @JsonIgnore
    @ApiModelProperty(value = "删除标记，false标识未删除，true标识删除,前台不传", hidden = true)
    private Boolean deleteFlag;

    /**
     * 实体插入前 基本字段赋值
     */
    public void preInsert() {
        this.id = IdUtil.fastSimpleUUID();
        this.createTime = new Date();
        this.updateBy = this.createBy;
        this.updateTime = this.createTime;
        this.deleteFlag = false;
    }

    /**
     * 实体更新前 基本字段赋值
     */
    public void preUpdate() {
        this.createBy = null;
        this.createTime = null;
        this.updateTime = new Date();
        this.deleteFlag = null;
    }

    /**
     * 实体删除前 基本字段赋值
     */
    public void preDelete(String id) {
        this.id = id;
        this.createBy = null;
        this.createTime = null;
        this.updateTime = new Date();
        this.deleteFlag = true;
    }

}
